﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConjureBivouac2012.GameStates;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace ConjureBivouac2012.Object
{
    public class AbstractPlayer : Entity
    {
        protected AbstractVehicule vehicule;
        protected PlayerIndex playerIndex;

        private const int movePerSecond = 120;
        
        public AbstractPlayer(PlayerIndex playerIndex)
        {
            this.playerIndex = playerIndex;
            this.Initialize();
        }

        public AbstractVehicule GetVehicule()
        {
            return vehicule;
        }

        public override void Update(GameTime gameTime)
        {
            if (vehicule.GetAnimationCount() == 0)
            {
                GamePadState aGamePad = GamePad.GetState(playerIndex);
                KeyboardState aKeyboard = Keyboard.GetState();

                Vector2 dir = Vector2.Zero;

                if(aGamePad.IsConnected)
                {
                    dir = UpdateGamePadMove(aGamePad);
                }
                else
                {
                    dir = UpdateKeyboardMove(aKeyboard);
                }

                Vector2 pos = vehicule.sprite.Position;

                vehicule.sprite.setPosition(PosWithinBounds(gameTime, dir, pos));
            }
        }

        private Vector2 UpdateGamePadMove(GamePadState aGamePad)
        {
            return new Vector2(aGamePad.ThumbSticks.Left.X, -aGamePad.ThumbSticks.Left.Y);
        }

        private Vector2 UpdateKeyboardMove(KeyboardState aKeyboard)
        {
            Vector2 dir = new Vector2(0, 0);

            if (aKeyboard.IsKeyDown(Keys.S) == true)
            {
                dir.Y = 1;
            }
            if (aKeyboard.IsKeyDown(Keys.A) == true)
            {
                dir.X = -1;
            }
            if (aKeyboard.IsKeyDown(Keys.W) == true)
            {
                dir.Y = -1;
            }
            if (aKeyboard.IsKeyDown(Keys.D) == true)
            {
                dir.X = 1;
            }

            return dir;
        }

        private Vector2 PosWithinBounds(GameTime time, Vector2 dir, Vector2 pos)
        {
            Vector2 newPos = pos + dir * movePerSecond * (float)time.ElapsedGameTime.TotalSeconds;

            float leftBound = GetVehicule().GetRadius();
            float topBound = GetVehicule().GetRadius();

            float rightBound = GameInfo.GetInstance().GetDevice().Viewport.Width - GetVehicule().GetRadius();
            float bottomBound = GameInfo.GetInstance().GetDevice().Viewport.Height - GetVehicule().GetRadius();

            if(newPos.X > rightBound)
            {
                newPos.X = rightBound;
            }
            else if(newPos.X < leftBound)
            {
                newPos.X = leftBound;
            }

            if (newPos.Y > bottomBound)
            {
                newPos.Y = bottomBound;
            }
            else if (newPos.Y < topBound)
            {
                newPos.Y = topBound;
            }

            return newPos;
        }
    }
}
